{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[-0.5795866  -0.82742887  0.02302219 -0.66366802]\n",
      " [-0.87534412 -0.43165541 -0.9966046   0.35897981]\n",
      " [ 0.37435929 -0.11961625  0.02429239 -0.5647008 ]]\n",
      "[[ 0.23559851]\n",
      " [-0.86258099]\n",
      " [-0.81063997]\n",
      " [-0.03302228]]\n"
     ]
    }
   ],
   "source": [
    "#输入数据\n",
    "X = np.array([[1,0,0],  # 异或问题的四个输入\n",
    "              [1,0,1],\n",
    "              [1,1,0],\n",
    "              [1,1,1]])\n",
    "#标签\n",
    "Y = np.array([[0,1,1,0]])  # 2维的array，激活函数sigmoid函数，取值范围0-1\n",
    "\n",
    "#权值初始化，取值范围-1到1\n",
    "V = np.random.random((3,4))*2-1 \n",
    "W = np.random.random((4,1))*2-1\n",
    "print(V)\n",
    "print(W)\n",
    "\n",
    "#学习率设置\n",
    "lr = 0.11\n",
    "\n",
    "def sigmoid(x):\n",
    "    return 1/(1+np.exp(-x))\n",
    "\n",
    "def dsigmoid(x):\n",
    "    return x*(1-x)\n",
    "\n",
    "def update():\n",
    "    global X,Y,W,V,lr\n",
    "    \n",
    "    L1 = sigmoid(np.dot(X,V))  # 隐藏层输出(4,4),x输入的矩阵X，权值的矩阵V\n",
    "    L2 = sigmoid(np.dot(L1,W))  # 输出层输出(4,1)，隐藏层的输入L1，隐藏层和输出层的权重W\n",
    "    \n",
    "    L2_delta = (Y.T - L2)*dsigmoid(L2)  # 这个见人工神经网络的公式\n",
    "    L1_delta = L2_delta.dot(W.T)*dsigmoid(L1)\n",
    "    \n",
    "    W_C = lr*L1.T.dot(L2_delta) # W的改变\n",
    "    V_C = lr*X.T.dot(L1_delta)\n",
    "    \n",
    "    W = W + W_C\n",
    "    V = V + V_C"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Error: 0.4995757867518793\n",
      "Error: 0.4955937394274951\n",
      "Error: 0.4848325461384726\n",
      "Error: 0.44501048887232925\n",
      "Error: 0.3610704439234838\n",
      "Error: 0.27959348187404176\n",
      "Error: 0.22460809585014554\n",
      "Error: 0.1455175567793116\n",
      "Error: 0.11202099253704148\n",
      "Error: 0.09304426274336858\n",
      "Error: 0.08068098927784596\n",
      "Error: 0.0718937831360954\n",
      "Error: 0.06527191979446303\n",
      "Error: 0.0600693556451906\n",
      "Error: 0.055852680029503776\n",
      "Error: 0.05235176158438612\n",
      "Error: 0.049388829796818516\n",
      "Error: 0.04684172963002776\n",
      "Error: 0.04462351921934286\n",
      "Error: 0.04267048908025182\n",
      "Error: 0.04093479349603861\n",
      "Error: 0.03937973903775786\n",
      "Error: 0.03797667073155507\n",
      "Error: 0.036702854602770396\n",
      "Error: 0.03554000157764083\n",
      "Error: 0.034473215786668135\n",
      "Error: 0.033490230627971816\n",
      "Error: 0.03258084420841991\n",
      "Error: 0.03173649562331419\n",
      "Error: 0.030949942468800624\n",
      "Error: 0.03021501227268021\n",
      "Error: 0.029526408676381222\n",
      "Error: 0.02887955870370229\n",
      "Error: 0.028270491233201467\n",
      "Error: 0.027695739430598998\n",
      "Error: 0.027152261766898805\n",
      "Error: 0.026637377589597052\n",
      "Error: 0.02614871418925369\n",
      "Error: 0.02568416302026243\n",
      "Error: 0.02524184326698045\n",
      "[[0.01701804]\n",
      " [0.97377858]\n",
      " [0.97764652]\n",
      " [0.03369064]]\n",
      "0\n",
      "1\n",
      "1\n",
      "0\n"
     ]
    }
   ],
   "source": [
    "for i in range(20000):\n",
    "    update() # 更新权值\n",
    "    if i%500==0:  # 每500次观察误差变化\n",
    "        L1 = sigmoid(np.dot(X,V))#隐藏层输出(4,4)\n",
    "        L2 = sigmoid(np.dot(L1,W))#输出层输出(4,1)\n",
    "        print('Error:',np.mean(np.abs(Y.T-L2))) #mean求平均值，理想值-实际输出再正数abs\n",
    "        \n",
    "L1 = sigmoid(np.dot(X,V))#隐藏层输出(4,4)\n",
    "L2 = sigmoid(np.dot(L1,W))#输出层输出(4,1)\n",
    "print(L2)\n",
    "\n",
    "def judge(x):\n",
    "    if x>=0.5:\n",
    "        return 1\n",
    "    else:\n",
    "        return 0\n",
    "# map函数接收函数 f 和 list，并通过把函数 f 依次作用在 list 的每个元素上，\n",
    "# 得到一个新的object并返回。Python3返回迭代对象）\n",
    "# 将L2依次带入函数运算，再输出迭代对象\n",
    "for i in map(judge,L2):\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
